package config;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.Random;

/**
 * @author admin
 */
public class Price {

    /**
     * 默认单价
     */
    private BigDecimal initPrice;

    /**
     * 当前单价
     */
    private BigDecimal currentPrice;

    /**
     * 单价区间
     */
    private BigDecimal interval;

    public Price(BigDecimal initPrice, BigDecimal interval) {
        this.initPrice = initPrice;
        this.currentPrice = initPrice;
        this.interval = interval;
    }

    public BigDecimal getInitPrice() {
        return initPrice;
    }

    public void setInitPrice(BigDecimal initPrice) {
        this.initPrice = initPrice;
    }

    public BigDecimal getCurrentPrice() {
        return currentPrice;
    }

    public void setCurrentPrice(BigDecimal currentPrice) {
        this.currentPrice = currentPrice;
    }

    public BigDecimal getInterval() {
        return interval;
    }

    public void setInterval(BigDecimal interval) {
        this.interval = interval;
    }

    @Override
    public String toString() {
        return "bean.Price{" +
                "初始价格=" + initPrice +
                ", 当前价格=" + currentPrice +
                ", 浮动=" + interval +
                '}';
    }

    /**
     * 当前价格 = 默认 - 浮动 + (0-浮动*2)
     * @return
     */
    public Price rand() {
        float v = new Random().nextFloat() * (interval.floatValue() * 2);
//        System.out.println("随机数:"+v);
        String format = new DecimalFormat("#.00").format(v);
//        System.out.println("保留两位小数:"+format);

        BigDecimal fu = new BigDecimal(format);
        currentPrice = initPrice.subtract(interval).add(fu);

        return this;
    }
}
